//
//  YYImageCollectionViewCell.swift
//  CodeLibrary_Swift
//
//  Created by 陆文宇 on 2017/2/9.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class YYZoomImageView: UIView, UIScrollViewDelegate {
    
    let scrollView: UIScrollView = {
        let _scrollView: UIScrollView = UIScrollView(frame: CGRect.zero)
        _scrollView.maximumZoomScale = 2
        _scrollView.showsVerticalScrollIndicator = false
        _scrollView.showsHorizontalScrollIndicator = false
        if #available(iOS 11.0, *) {
            _scrollView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        return _scrollView
    }()
    let imageView: UIImageView = {
        let _imageView: UIImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height))
        _imageView.contentMode = .scaleAspectFill
        _imageView.clipsToBounds = true
        return _imageView
    }()
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        scrollView.addSubview(imageView)
        scrollView.delegate = self
        addSubview(scrollView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        scrollView.frame = bounds
    }
    
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        let viewport = CGRect(x: 0, y: 0, width: scrollView.bounds.size.width, height: scrollView.bounds.size.height)
        
        let contentView = imageView
        layoutIfNeeded()
        let contentViewFrame = convert(contentView.frame, from: contentView.superview)
        var contentInset = UIEdgeInsets.zero
        
        contentInset.top = viewport.minY
        contentInset.left = viewport.minX
        contentInset.right = bounds.width - viewport.maxX
        contentInset.bottom = bounds.height - viewport.maxY
        
        if viewport.height > contentViewFrame.height {
            contentInset.top = floor(viewport.midY - contentViewFrame.height / 2.0)
            contentInset.bottom = floor(bounds.height - viewport.midY - contentViewFrame.height / 2.0)
        }
        
        if viewport.width > contentViewFrame.width {
            contentInset.left = floor(viewport.midX - contentViewFrame.width / 2.0)
            contentInset.right = floor(bounds.width - viewport.midX - contentViewFrame.width / 2.0)
        }
        
        contentView.yy_origin = CGPoint.zero
        scrollView.contentInset = contentInset
        scrollView.contentSize = contentView.frame.size
    }
}

class YYImageCollectionViewCell: UICollectionViewCell {
    
    var zoomImageView = YYZoomImageView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        contentView.addSubview(zoomImageView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        zoomImageView.frame = contentView.frame
    }
}

